<?php
/**
 * 问答
*/
apf_require_class("Bll_BLL");
apf_require_class("Const_Util");
class Bll_Qa_Question extends BLL{


    /**
     * 获取一条问答
     * @param boolean $is_get_file 是否获取附件
     * @return array answer 和 question:
     */
    public function get_question_one($qu_id,$is_get_file=false){
        $result = array();
        if($qu_id && is_numeric($qu_id)){
            $question = $this->_get_dao_question()->find_by_id($qu_id);
            if(empty($question)){
                return $result;
            }
            if($question['is_delete']==Const_Util::IS_DELETE_YES){
                return $result;
            }
            $answer = $this->_get_dao_answer()->find_by_id($qu_id);
            if($is_get_file && $question['file']){
                $question['file_url'] = "";
            }
            $result = empty($answer)?$question:array_merge($question,$answer);
        }
        return $result;
    }

    /**
     * 获取问题列表
     * @param int $type 一级分类
     * @param int $cate 二级分类
     * @param boolean $is_get_file 是否获取附件
     * @param boolean $is_get_answer 是否获取回答
     * @return question
     */
    public function get_question_list($type,$cate,$is_get_file=false,$is_get_answer=false,$row=2,$page=1){
        $result = array();
        if($type && $cate && is_numeric($type) && is_numeric($cate)){
            $where = array(
                    'type'=>$type,
                    'cate'=>$cate
                    );
            $page = $page<1?1:$page;
            $order = 'hits desc';
            $result = $this->_get_dao_question()->find_assoc($where,$order,$row,($page-1)*$row);
            $result = $this->_remove_delete($result);
            foreach($result as $id=>$question){
                if($is_get_file && $question['file']){
                    $result[$id]['file_url'] = "";
                }
            }
            //获取回答
            if($is_get_answer && !empty($result)){
                $in_list = implode(",",array_keys($result));
                $answer_condition = array(
                        "qu_id in ({$in_list}) and 1=?" => 1
                );
                $answer_list = $this->_get_dao_answer()->find($answer_condition);
                //将回答数据添加到结果集中
                foreach ($answer_list as $key => $value) {
                    $result[$value['qu_id']]['content'] = $value['content'];
                }
            }

        }
        return $result;
    }

    /**
     * 搜索
     * @param int $type 一级分类
     * @param string $search_text
     * @return question
     */
    public function get_question_search($type,$search_text){
        $result = array();
        $search_text = trim($search_text);
        if($search_text && $type && is_numeric($type)){
            $where = array(
                    'type'=>$type,
                    "title like '%{$search_text}%' AND 1=?"=>1
            );
            $order = 'created desc';
            $result = $this->_get_dao_question()->find_assoc($where,$order);
            $result = $this->_remove_delete($result);
            if(!empty($result)){
                foreach($result as $id=>$question){
                    if($question['file']){
                        $result[$id]['file_url'] = "";
                    }
                }
            }
        }
        //获取回答
        if(empty($result)){
            return $result;
        }
        $in_list = implode(",",array_keys($result));
        $answer_condition = array(
                "qu_id in ({$in_list}) and 1=?" => 1
        );
        $answer_list = $this->_get_dao_answer()->find($answer_condition);
        //将回答数据添加到结果集中
        foreach ($answer_list as $key => $value) {
            $result[$value['qu_id']]['content'] = $value['content'];
        }
        return $result;
    }
    /**
     * 删除问答
     * Enter description here ...
     * @param unknown_type $qu_id
     */
    public function delete_question($qu_id){
        if($qu_id && is_numeric($qu_id)){
            $this->_get_dao_question()->update_by_id($qu_id, array('is_delete'=>Const_Util::IS_DELETE_YES));
        }
    }

    /**
     *
     * 更新数据
     * @param unknown_type $id
     * @param unknown_type $title
     * @param unknown_type $user
     */
    public function update_question($id,$title,$file,$hits,$user){
        if(!empty($title)){
            $data['title'] = $title;
        }
        if(!empty($file)){
            $data['file'] = $file;
        }
        if(!empty($hits)){
            $data['hits'] = $hits;
        }
        $data['updater'] = $user;
        $data['updated'] = time();
        return $this->_get_dao_question()->update_by_id($id, $data);
    }
    public function update_answer($qid,$content){
        $status = false;
        if(empty($content)){
            return $status;
        }
        $up_answer = array(
                'content'=>$content
        );
        $this->_get_dao_answer()->update($up_answer,array('qu_id'=>$qid));
    }

    /**
     * 点击率
     * @param int $qu_id
     */
    public function add_hits($qu_id){
        $bool = false;
        if($qu_id && is_numeric($qu_id)){
            $q = $this->get_question_one($qu_id);
            if($q){
                $this->_get_dao_question()->update_by_id($qu_id, array('hits'=>$q['hits']+1));
                $bool = true;
            }
        }
        return $bool;
    }

    public function insert_answer($qid,$content){
        $status = false;
        if(empty($content) || empty($qid)){
            return $status;
        }
        $insert = array(
                'qu_id'=>$qid,
                'content'=>$content
                );
        return $this->_get_dao_answer()->insert($insert);
    }
    /**
     * 添加问题
     * Enter description here ...
     * @param unknown_type $data
     */
    public function add_question($type,$cate,$title,$file,$user_id){
        $time = time();
        $data['type'] = $type;
        $data['cate'] = $cate;
        $data['title'] = $title;
        $data['user_id'] = $user_id;
        $data['updater'] = $user_id;
        $data['created'] = $time;
        $data['updated'] = $time;
        $data['file'] = $file;
        return $this->_get_dao_question()->insert($data);
    }
    /**
     * 获取回答
     * @param int $qu_id
     * @return answer
     */
    public function get_answer_one($qu_id){
        $result = array();
        if($qu_id && is_numeric($qu_id)){
            $result = $this->_get_dao_answer()->find_by_id($qu_id);
        }
        return $result;
    }
    /**
     * 移除删除数据
     * is_delete=1
     */
    private function _remove_delete($data){
        $result = array();
        if(!empty($data)){
            foreach($data as $index=>$one){
                if($one['is_delete']==Const_Util::IS_DELETE_NO){
                    $result[$index] = $one;
                }
            }
        }
        return $result;
    }
    /**
     * @return DAO
     */
    private function _get_dao_question(){
        $dao = $this->get_dao('Dao_Qa_Question');
        return $dao;
    }
    /**
     * @return DAO
     */
    private function _get_dao_answer(){
        $dao = $this->get_dao('Dao_Qa_Answer');
        return $dao;
    }
}